package com.jinyang.baserouter.interceptor

import android.content.Context
import com.jinyang.baserouter.log
import com.alibaba.android.arouter.facade.template.IInterceptor
import com.alibaba.android.arouter.facade.Postcard
import com.alibaba.android.arouter.facade.annotation.Interceptor
import com.alibaba.android.arouter.facade.callback.InterceptorCallback
import com.jinyang.baserouter.PATH_ACTIVITY_WEB
import com.jinyang.baserouter.utils.ApplicationUtil

/**
 * @Author: LiuJinYang
 * @CreateDate: 2021/12/8
 * @Description:拦截器实现登录校验
 */
@Interceptor(priority = 1)
class LoginInterceptor : IInterceptor {
    private var context: Context? = null
    private var postcard: Postcard? = null
    private var callback: InterceptorCallback? = null
    override fun init(context: Context) {
        log("LoginInterceptor.init")
        this.context = context
    }

    override fun process(postcard: Postcard, callback: InterceptorCallback) {
        log("LoginInterceptor.process")
        this.postcard = postcard
        this.callback = callback
        if (postcard.path == PATH_ACTIVITY_WEB) {
            if (ApplicationUtil.instance.isLogin) {
                callback.onContinue(postcard)
                log("已登录")
            } else {
                callback.onInterrupt(Throwable("还没有登录，去登陆"))
                log("请先登录")
            }
        } else {
                log("LoginInterceptor.process: path不匹配，无需拦截")
            callback.onContinue(postcard)
        }
    }
}